// pages/my/myDetail/myDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userLogin: false,
    config: {},
    userInfo: {},
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  baseInfo() {
    let that = this;
    app.req({
      url: "/common/static/base",
      success(res) {
        if (200 == res.code) {
          that.toggle("config", res.result);
        }
      }
    })
  },
  toFunction(e) {
    let that = this;
    var f;
    switch (e.currentTarget.dataset.type) {
      case "order":
        f = this.order;
        break;
      case "points":
        f = this.points;
        break;
      case "fapiao":
        f = this.fapiao;
        break;
    }
    that.login(function (res) {
      f();
    }, true)
  },
  order() {
    wx.navigateTo({
      url: '/pages/order/orderList/orderList',
    })
  },
  points() {
    wx.navigateTo({
      url: '/pages/my/myPoints/myPoints',
    })
  },
  mall() {
    wx.navigateTo({
      url: '/pages/mall/mallList/mallList',
    })
  },
  fapiao() {
    wx.navigateTo({
      url: '/pages/my/myTicket/myTicket',
    })
  },
  login(f, login) {
    app.getUserInfo({
      success(res) {
        f && f(res);
      },
      error() {
        if (login) {
          wx.showModal({
            title: '提示',
            content: '请您登录之后继续操作',
            confirmText: '登录',
            success({
              confirm
            }) {
              if (confirm) {
                wx.navigateTo({
                  url: '/pages/login/login',
                })
              }
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  beforeAction(f) {
    let that = this;
    if (this.data.userLogin) {
      f();
    } else {
      that.login(function (res) {
        that.toggle("userLogin", true);
        that.toggle("userInfo", res);
      }, true)
    }
  },
  editInfo() {
    let that = this;
    that.beforeAction(function () {
      wx.navigateTo({
        url: '/pages/my/myEdit/myEdit',
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.login(function (res) {
      that.toggle("userLogin", true);
      that.toggle("userInfo", res);
      that.baseInfo();
    })
  },

  call(e) {
    wx.makePhoneCall({
      phoneNumber: '13156878738',
      success: (result) => {},
      fail: () => {},
      complete: () => {}
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.baseInfo();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})